/**
 * Useful links:
 * How to extract Document Term Vector in Lucene 3.5.0
 * 	http://stackoverflow.com/questions/8776794/how-to-extract-document-term-vector-in-lucene-3-5-0
 * Get term frequencies in Lucene
 * 	http://stackoverflow.com/questions/667389/get-term-frequencies-in-lucene
 * Measures customization in Lucene
 * 	http://www.lucenetutorial.com/advanced-topics/scoring.html
 * Example of idf
 * 	http://lucene.472066.n3.nabble.com/Max-Frequency-and-Tf-Idf-td539006.html
 * 	http://lucene.472066.n3.nabble.com/Getting-DF-amp-IDF-td547386.html
 * Lucene code examples
 * 	http://www.cnlp.org/apachecon2005/
 * Other implementations of Lucene
 * 	http://128.206.119.157/nlp/papers/NLPTools.pdf
 * Lemmatization tools
 * 	https://github.com/larsmans/lucene-stanford-lemmatizer
 * Create your own analyzer for lucene
 * 	http://wiki.apache.org/lucene-java/LuceneFAQ
 * Advance topics about Lucene Analizer
 * 	http://www.ibm.com/developerworks/opensource/library/os-apache-lucenesearch/
 * 	http://www.sis.pitt.edu/~jjiang/lucene-ex/TutorialAnalyzer.java
 * 	http://whiteboardjunkie.wordpress.com/tag/custom-analyzer/
 * 	http://stackoverflow.com/questions/6293354/java-lucene-custom-analyzer-and-tokenizer-creating-problem-in-termvector-offsets
 */
package indexing;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.lucene.analysis.PerFieldAnalyzerWrapper;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexReader.FieldOption;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.util.Version;
/**
 * @author Sebastian Suarez Benjumea "ssuarezbe@gmail.com"
 */
public class LuceneIndex {

	/**
	 * Attributes
	 */
	public StandardAnalyzer analyzer;
	public PerFieldAnalyzerWrapper aWrapper;
	public FSDirectory dir;
	public IndexWriterConfig config;
	public IndexWriter w;
	//Attributes used in the method "addDoc()"
	Document doc;
	
	public void init(String indexDir)throws Exception{
		/**
		 * Example of use of PerFieldAnalyzer:
		 * 	http://lucene.472066.n3.nabble.com/PerFieldAnalyzerWrapper-use-Analyzer-s-not-being-used-as-expected-td541413.html
		 * new way
		 * 	http://lucene.apache.org/core/old_versioned_docs/versions/3_5_0/api/core/org/apache/lucene/analysis/PerFieldAnalyzerWrapper.html
		 */
		Map analyzerPerField = new HashMap();
		analyzerPerField.put("filename", new WhitespaceAnalyzer(Version.LUCENE_35));
		this.analyzer = new StandardAnalyzer(Version.LUCENE_35);
		this.aWrapper =new PerFieldAnalyzerWrapper(this.analyzer, analyzerPerField);
		this.dir = FSDirectory.open(new File(indexDir));
		this.config = new IndexWriterConfig(Version.LUCENE_35, this.aWrapper);
		this.w=new IndexWriter(this.dir, this.config);
	}
	/**
	 * This method index a document, where the document refers to a sentence. The sentence fields are body and filename.
	 * The filename refers to the filename which it belongs. 
	 */
	public void addDoc(String textBody,String filename) throws IOException {
	    this.doc = new Document();
	    this.doc.add((Fieldable) new Field("filename", filename, Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.YES));
	    this.doc.add((Fieldable) new Field("body", textBody, Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.YES));
	    this.w.addDocument(this.doc);
	    textBody=null;
	}
	
	public void closeIndexWriter()throws IOException{
		this.w.close();
	}
}
